/**
 * @todo
 */
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#include "ui.h"

void conv_base(int num, int result[], int mark, int t_base)
{
    if (num < t_base) {
        result[mark] = num;
        return;
    } else {
        result[mark] = num % t_base;
        conv_base(num / t_base, result, ++mark, t_base);
    }
}

void conv_from_decimal()
{
    int
    mark = 0,
    num = 0,
    result[16] = {0};
    unsigned char t_base = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    // Draw UI
    ui_cls();
    printf("Please start a conversion beginning with a operand within [0 ~ 32767]: ");
    scanf("%d", &num);

    printf("Please enter the target base: ");
    scanf("%u", &t_base);

    for (unsigned char i = 0; i < 16; i++)
    {
        result[i] = t_base;
    }

    conv_base(num, result, mark, t_base);

    // Print the result
    printf("Result in %d-base is: ", t_base);

    for (num = 15; num >=0; num--) {
        if (result[num] < t_base) {
            if (result[num] > 9) {
                unsigned char x;
                unsigned int t;

                x = 'A';
                t = result[num] - 10;
                x += t;

                printf("%c", x);
            } else {
                printf("%c", result[num]);
            }
        }
    }
    putchar('\n');
    system("PAUSE");


    printf("Continue or not? Yes [y], No [n]?\n");
    scanf("%c", &inputed);

    switch (inputed)
    {
    case 'Y':
    case 'y':
        is_continue = 1;
        break;
    case 'N':
    case 'n':
        is_continue = 0;
        break;
    default:
        ; // TODO
    }

    if (is_continue)
    {
        conv_from_decimal();
    }
    else
    {
        op_conv_init();
    }
}

int conv_base10(int n, int x, int c)
{
    int t = 1;

    for (unsigned int j = 1; j < n; j++) {
        t *= c;
    }

    return x * t;
}

void conv_to_decimal()
{
    int
    c,
    c1 = 0,
    sum = 0;

    unsigned char
    inputed = 0,
    is_continue = 0;

    // Draw UI
    ui_cls();
    printf("Please enter original base: ");
    scanf("%d", &c);

    char a[10] = {0};
    int b[10] = {0};

    // TODO
    for (unsigned char i = 0; i < 10; i++) {
        a[i] = 'x';
    }

    // Input
    printf("Please enter operand, ends with `=': ");
    for (unsigned char i = 0; i < 10; i++) {
        scanf("%d", &a[i]);

        if (a[i] == '=') {
            break;
        }
    }

    // Get rid of `x'
    for (unsigned char i = 0; i < 10; i++) {
        if (a[i] != 'x') {
            c1++;
        }
    }

    for (unsigned char i = 0; i < c1 - 1; i++) {
        if (a[i] >= 'A' && a[i] <= 'F') {
            b[i] = a[i] - 'A';
            b[i] += 10;
        } else {
            b[i] = a[i] - '0';
        }

        if (b[i] >= c) {
            printf("Input error: every digit could not be equal nor large than %d", c);

            system("PAUSE");


            printf("Continue or not? Yes [y], No [n]?\n");
            scanf("%c", &inputed);

            switch (inputed)
            {
            case 'Y':
            case 'y':
                is_continue = 1;
                break;
            case 'N':
            case 'n':
                is_continue = 0;
                break;
            default:
                ; // TODO
            }

            if (is_continue)
            {
                conv_to_decimal();
            }
            else
            {
                op_conv_init();
            }
        }

        sum += conv_base10(c1 - i - 1, b[i], c);
    }

    printf("%d", sum);

    system("PAUSE");


    printf("Continue or not? Yes [y], No [n]?\n");
    scanf("%c", &inputed);

    switch (inputed)
    {
    case 'Y':
    case 'y':
        is_continue = 1;
        break;
    case 'N':
    case 'n':
        is_continue = 0;
        break;
    default:
        ; // TODO
    }

    if (is_continue)
    {
        conv_to_decimal();
    }
    else
    {
        op_conv_init();
    }
}

/**
 * Function selection.
 */
void op_conv_init()
{
    unsigned char selected;

    // Draw UI
    ui_cls();
    ui_add_empty_line(1);
    ui_add_border_line_text("Number conversion selection");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("1: From decimal");
    ui_add_border_row_empty(1);
    ui_add_opt_desc("2: To decimal");
    ui_add_border_row_empty(2);
    ui_add_opt_desc("3: Back to calculator start");
    ui_add_border_row_empty(1);
    ui_add_border_line_single();
    ui_add_empty_line(1);
    printf("Please enter the operation number you want [1 ~ 3]: ");

    scanf("%d", &selected);
    switch (selected)
    {
    case 1:
        conv_from_decimal();
        break;
    case 2:
        conv_to_decimal();
        break;
    case 3:
        index_start();
        break;
    default:
        ui_cls();
        printf("What you had selected is %d, which is incorrect.\n", selected);
        system("PAUSE");
        op_conv_init();
    }
}
